package ynu.edu.online_course.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import ynu.edu.online_course.entity.WatchRecord;
import ynu.edu.online_course.service.WatchRecordService;

import java.util.Map;


@RestController
@RequestMapping("/watch-records")
public class WatchRecordController {

    @Autowired
    private WatchRecordService watchRecordService;

    @PostMapping
    public ResponseEntity<?> addWatchRecord(@RequestBody WatchRecord watchRecord) {
        watchRecordService.addWatchRecord(watchRecord);
        return ResponseEntity.ok("Watch record added successfully");
    }

    @GetMapping("/user/{userId}/duration")
    public ResponseEntity<?> getTotalWatchDuration(@PathVariable Long userId) {
        return ResponseEntity.ok(watchRecordService.getTotalWatchDuration(userId));
    }

    @GetMapping("/user/{userId}")
    public ResponseEntity<?> getUserWatchRecords(@PathVariable Long userId) {
        return ResponseEntity.ok(watchRecordService.getUserWatchRecords(userId));
    }

    // 获取章节观看时长
    @GetMapping("/chapter-durations")
    public ResponseEntity<Map<Long, Long>> getChapterDurations() {
        return ResponseEntity.ok(watchRecordService.getChapterWatchDurations());
    }

    // 获取课程观看时长
    @GetMapping("/course-durations")
    public ResponseEntity<Map<Long, Long>> getCourseDurations() {
        return ResponseEntity.ok(watchRecordService.getCourseWatchDurations());
    }
}
